/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager.discovery;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.Serializable;

public class ContentBundle implements Serializable {

    private String shortDescription;

    private String longDescription;

    private String icon;

    private String displayName;

    private String contentURI;

    private String termsAndConditions;

    private String price;

    private String category;

    private String reviewCount;

    private int rating;

    private String vendorName;

    private String bundleId;

    private String iconFileUri;
    
    private String version;

    private String creationDate;

    public String getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(String creationDate) {
        this.creationDate = creationDate;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getIconFileUri() {
        if(iconFileUri == null) {
            iconFileUri = "";
        }
        return iconFileUri;
    }

    public void setIconFileUri(String iconFileUri) {
        this.iconFileUri = iconFileUri;
    }

    public String getBundleId() {
        return bundleId;
    }

    public void setBundleId(String bundleId) {
        this.bundleId = bundleId;
    }

    public String getVendorName() {
        return vendorName;
    }

    public void setVendorName(String vendorName) {
        this.vendorName = vendorName;
    }

    public String getPrice() {
        if (price == null) {
            price = "Free";
        }
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getReviewCount() {
        return reviewCount;
    }

    public void setReviewCount(String reviewCount) {
        this.reviewCount = reviewCount;
    }

    public int getRating() {
        return rating;
    }

    public void setRating(int rating) {
        this.rating = rating;
    }

    public String getTermsAndConditions() {
        if(termsAndConditions == null) {
            termsAndConditions = "No terms and conditions specified by the content provider";
        }
        return termsAndConditions;
    }

    public void setTermsAndConditions(String termsAndConditions) {
        this.termsAndConditions = termsAndConditions;
    }

    public String getLongDescription() {
        if(longDescription == null) {
            longDescription = shortDescription;
        }
        return longDescription;
    }

    public void setLongDescription(String longDescription) {
        this.longDescription = longDescription;
    }

    public String getContentURI() {
        return contentURI;
    }

    public void setContentURI(String contentURI) {
        this.contentURI = contentURI;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public static ContentBundle marshalFrom(KXmlParser parser) throws IOException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }
        ContentBundle contentBundle = new ContentBundle();
        try {
            parser.require(XmlPullParser.START_TAG, null, "bundle");
            while (parser.nextTag() == XmlPullParser.START_TAG) {
                if (parser.getName().equals("displayName")) {
                    contentBundle.setDisplayName(parser.nextText());
                } else if (parser.getName().equals("icon")) {
                    contentBundle.setIcon(parser.nextText());
                } else if (parser.getName().equals("shortDescription")) {
                    contentBundle.setShortDescription(parser.nextText());
                } else if (parser.getName().equals("contentURI")) {
                    contentBundle.setContentURI(parser.nextText());
                } else if (parser.getName().equals("longDescription")) {
                    contentBundle.setLongDescription((parser.nextText()));
                } else if (parser.getName().equals("termsAndConditions")) {
                    contentBundle.setTermsAndConditions((parser.nextText()));
                } else if (parser.getName().equals("price")) {
                    contentBundle.setPrice((parser.nextText()));
                } else if (parser.getName().equals("vendorName")) {
                    contentBundle.setVendorName((parser.nextText()));
                } else if (parser.getName().equals("bundleId")) {
                    contentBundle.setBundleId((parser.nextText()));
                } else if (parser.getName().equals("version")) {
                    contentBundle.setVersion((parser.nextText()));
                } else if (parser.getName().equals("category")) {
                    contentBundle.setCategory((parser.nextText()));
                } else if (parser.getName().equals("creationDate")) {
                    contentBundle.setCreationDate((parser.nextText()));
                }
            }

        } catch (XmlPullParserException e) {
            throw new IOException("JV-000-010:" + e.toString());
        }
        return contentBundle;
    }

}
